 #include <FastLED.h>
#include <Servo.h>

#define LED1_PIN    4  //D2
#define LED2_PIN    5
#define COLOR_ORDER GRB
#define NUM_LEDS    20

CRGB RGBLEDS1[NUM_LEDS];
CRGB RGBLEDS2[NUM_LEDS];

uint8_t brightness = 128;
uint16_t adc_value;
uint16_t led_num_on = 0;
uint16_t led_num_last = 0;
uint16_t adc_val[10] = {0};
uint8_t rgb_index;
uint8_t run_mode = 0;


//300 30ms = 9s
//150 30ms = 4.5s
//600
#define RUNMODE_TIMEOUT    300//9s
uint16_t run_mode_timeout = RUNMODE_TIMEOUT;

Servo myservo;
#define FEED_TIME    1000
void rgb_blink()
{
  uint8_t i;
  for(i = 0; i < 8; i++)
  {
    fill_solid(RGBLEDS1, 20, CRGB::White);
    fill_solid(RGBLEDS2, 20, CRGB::White);
    FastLED.show();
    delay(50);
    
    fill_solid(RGBLEDS1, 20, CRGB::Black);
    fill_solid(RGBLEDS2, 20, CRGB::Black);
    FastLED.show();
    delay(50);
  }
}

uint8_t get_led_num(uint16_t adc)
{
  uint8_t num = 0;
  if(adc < 100){
    num = 0;
  }
  else if(100 <= adc && adc < 130)
  {
    num = 1;
  }
  else if(130 <= adc && adc < 160)
  {
    num = 2;
  }
  else if(160 <= adc && adc < 190)
  {
    num = 3;
  }
  else if(190 <=adc && adc < 220)
  {
    num = 4;
  }
  else if(220 <= adc && adc < 250)
  {
    num = 5;
  }
  else if(250 <= adc && adc < 280)
  {
    num = 6;
  }
  else if(280 <=adc && adc < 310)
  {
    num = 7;
  }
  else if(310 <=adc && adc < 340)
  {
    num = 8;
  }
  else if(340 <=adc && adc < 370)
  {
    num = 9;
  }
  else if(370 <=adc && adc < 400)
  {
    num = 10;
  }
  else if(400 <=adc && adc < 430)
  {
    num = 11;
  }
  else if(430 <=adc && adc < 460)
  {
    num = 12;
  }
  else if(460 <=adc && adc < 500)
  {
    num = 13;
  }
  else if(500 <=adc && adc < 540)
  {
    num = 14;
  }
  else if(540 <=adc && adc < 590)
  {
    num = 15;
  }
  else if(590 <=adc && adc < 630)
  {
    num = 16;
  }
  else if(630 <=adc && adc < 670)
  {
    num = 17;
  }
  else if(670 <=adc && adc < 710)
  {
    num = 18;
  }
  else if(710 <=adc && adc < 750)
  {
    num = 19;
  }
  else if(adc >= 750)
  {
    num = 20;
  }
  
  return num;
}



void updata_leds_from_adc(uint16_t adc)
{
  led_num_on = get_led_num(adc);

  if(led_num_last  -  led_num_on > 6){//压差值大于4 才认为是放电
    rgb_blink();
    feed_bullet();
  }
  if(led_num_on > 0)
  {
    //White 240
    //red 0
    fill_gradient(RGBLEDS1, 0, CHSV(180, 255, 255) , led_num_on - 1, CHSV(180-80*led_num_on/20, 255, 255), LONGEST_HUES);
    fill_gradient(RGBLEDS2, 0, CHSV(180, 255, 255) , led_num_on - 1, CHSV(180-80*led_num_on/20, 255, 255), LONGEST_HUES);
    delay(10);
    FastLED.show();
    delay(10);
    run_mode_timeout = RUNMODE_TIMEOUT;
    run_mode = 0;
  }
  else{
    if(run_mode_timeout == 0){
      run_mode_timeout = RUNMODE_TIMEOUT;//9s
      run_mode++;
      if(run_mode == 9){
        run_mode = 1;
      }
    }
    if(run_mode == 0){
      fill_solid(RGBLEDS1, 20, CRGB::White);
      fill_solid(RGBLEDS2, 20, CRGB::White);
    }else if(run_mode == 1){
      fill_palette(RGBLEDS1, 20, rgb_index, 10, RainbowColors_p , 255, LINEARBLEND);
      fill_palette(RGBLEDS2, 20, rgb_index, 10, RainbowColors_p , 255, LINEARBLEND);
      rgb_index -= 20;
    }
    else if(run_mode == 7){
      fill_palette(RGBLEDS1, 20, rgb_index, 10, PartyColors_p , 255, LINEARBLEND);
      fill_palette(RGBLEDS2, 20, rgb_index, 10, PartyColors_p , 255, LINEARBLEND);
      rgb_index -= 20;
    }
    else if(run_mode == 3){
      fill_palette(RGBLEDS1, 20, rgb_index, 10, OceanColors_p , 255, LINEARBLEND);
      fill_palette(RGBLEDS2, 20, rgb_index, 10, OceanColors_p , 255, LINEARBLEND);
      rgb_index -= 20;
    }
    else if(run_mode == 4){
      fill_palette(RGBLEDS1, 20, rgb_index, 10, CloudColors_p , 255, LINEARBLEND);
      fill_palette(RGBLEDS2, 20, rgb_index, 10, CloudColors_p , 255, LINEARBLEND);
      rgb_index -= 15;
    }
    else if(run_mode == 5){
      fill_palette(RGBLEDS1, 20, rgb_index, 10, LavaColors_p , 255, LINEARBLEND);
      fill_palette(RGBLEDS2, 20, rgb_index, 10, LavaColors_p , 255, LINEARBLEND);
      rgb_index -= 10;
    }
    else if(run_mode == 6){
      fill_palette(RGBLEDS1, 20, rgb_index, 10, ForestColors_p , 255, LINEARBLEND);
      fill_palette(RGBLEDS2, 20, rgb_index, 10, ForestColors_p , 255, LINEARBLEND);
      rgb_index -= 25;
    }
    else if(run_mode == 7){
      fill_palette(RGBLEDS1, 20, rgb_index, 10, RainbowStripeColors_p , 255, LINEARBLEND);
      fill_palette(RGBLEDS2, 20, rgb_index, 10, RainbowStripeColors_p , 255, LINEARBLEND);
      rgb_index -= 15;
    }
    
    else if(run_mode == 8){
      fill_palette(RGBLEDS1, 20, rgb_index, 10, HeatColors_p , 255, LINEARBLEND);
      fill_palette(RGBLEDS2, 20, rgb_index, 10, HeatColors_p , 255, LINEARBLEND);
      rgb_index -= 10;
    }
    delay(10);
    FastLED.show();
  }
  led_num_last = led_num_on;
}

uint16_t get_adc_value()
{
  uint8_t i;
  //获取值
  uint16_t adc_min = 1000,adc_max = 0;
  uint16_t sum = 0;
  uint16_t adc_avg = 0;
  for(i = 0; i < 10; i++)
  {
    adc_val[i] = analogRead(A0);
    if(adc_val[i] > adc_max){
      adc_max = adc_val[i];
    }
    if(adc_val[i] < adc_min){
      adc_min = adc_val[i];
    }
    sum = sum + adc_val[i];
  }
  adc_avg = (sum - adc_min - adc_max)/8;
  return adc_avg;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  LEDS.addLeds<WS2812, LED1_PIN, COLOR_ORDER>(RGBLEDS1, NUM_LEDS);
  delay(20);
  LEDS.addLeds<WS2812, LED2_PIN, COLOR_ORDER>(RGBLEDS2, NUM_LEDS);
  delay(20);
  FastLED.setBrightness(brightness);
  delay(20);
  rgb_blink();
  delay(20);
  myservo.attach(12, 500, 2500);
  myservo.write(0);
  delay(FEED_TIME);
  feed_bullet();
}

void feed_bullet()
{
  myservo.write(180);
  delay(FEED_TIME);
  myservo.write(0);
  delay(FEED_TIME);
}

void loop() {
  int pos;
  adc_value = get_adc_value();
  updata_leds_from_adc(adc_value);
  //Serial.println(adc_value);
  delay(35);
  if(run_mode_timeout > 0){
    run_mode_timeout--;
  }
}
